import { createRouter, createWebHashHistory } from 'vue-router';
import { globalStore } from '@/store';
import BasicLayout from '../layouts/BasicLayout.vue';
import BlankLayout from '../layouts/BlankLayout.vue';
import UserLayout from '../layouts/UserLayout.vue';
import WelcomePage from '../views/WelcomeView.vue';

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      //path必须为'/',这个是pro-layout规定的，根据这个path来生成的menu
      path: '/',
      name: 'home',
      meta: { title: 'Home' },
      component: BasicLayout,
      redirect: { name: 'welcome' },
      children: [
        {
          path: '/welcome',
          name: 'welcome',
          meta: { title: '欢迎', icon: 'icon-home' },
          component: WelcomePage,
        },
        {
          path: '/bizDashboard',
          name: 'bizDashboard',
          meta: { title: '业务大盘', icon: 'icon-echart' },
          component: () => import('@/views/bizDashboard/BizDashboardConsole.vue'),
        },
        {
          path: '/jobList',
          name: 'jobList',
          meta: { title: '作业列表', icon: 'icon-viewlist' },
          component: () => import('@/views/job/JobConsole.vue'),
        },
        {
          path: '/jobLogList',
          name: 'jobLogList',
          meta: { title: '执行记录', icon: 'icon-calendar' },
          component: () => import('@/views/job/JobExecutionTable.vue'),
        },
        {
          path: '/tools',
          name: 'tools',
          meta: { title: '各种工具', icon: 'icon-tools-hardware' },
          component: BlankLayout,
          redirect: { name: 'datasourceDes' },
          children: [
            {
              path: 'datasource-des',
              name: 'datasourceDes',
              component: () => import('@/views/tools/DataSourceDes.vue'),
              meta: { title: '数据源加解密' },
            },
          ],
        },
        {
          path: '/user',
          name: 'user',
          meta: { title: '用户管理', icon: 'icon-Customermanagement' },
          component: () => import('@/views/user/UserConsole.vue'),
        },
        {
          path: '/demos',
          name: 'demos',
          meta: { title: '参考DEMO', icon: 'icon-viewgallery', flat: true },
          component: BlankLayout,
          redirect: () => ({ name: 'page1' }),
          children: [
            {
              path: 'page-1',
              name: 'page1',
              meta: { title: 'PageInfo' },
              component: () => import('../views/demo/PageInfo.vue'),
            },
            {
              path: 'page-2',
              name: 'page2',
              meta: { title: 'PageTypography' },
              component: () => import('../views/demo/PageTypography.vue'),
            },
            {
              path: 'dynamic-match/:id(\\d+)',
              name: 'dynamic-match',
              // 路由 path 默认参数再 meta.params 里
              meta: { title: 'DynamicMatch', params: { id: 1 } },
              component: () => import('../views/demo/DynamicMatch.vue'),
            },
            {
              path: 'version',
              name: 'version',
              meta: { title: 'Version' },
              component: () => import('../views/demo/Version.vue'),
            },
            {
              path: 'db-to-db',
              name: 'dbTodb',
              component: () => import('@/views/demo/EtlConsole.vue'),
              meta: { title: 'JSON编辑器' },
            },
            {
              path: '/data',
              name: 'data',
              meta: { title: '数据迁移', icon: 'icon-process' },
              component: BlankLayout,
              redirect: { name: 'datasourceDes' },
              children: [
                {
                  path: 'wxMember-oracle-to-mongo-seq',
                  name: 'wxMemberOracleToMongoSeq',
                  component: () => import('@/views/data/WxMemberOracleToMongoSeq.vue'),
                  meta: { title: 'WxMember序列' },
                },
                {
                  path: 'wxMember-oracle-to-mongo-snow',
                  name: 'wxMemberOracleToMongoSnow',
                  component: () => import('@/views/data/WxMemberOracleToMongoSnow.vue'),
                  meta: { title: 'WxMember分布式主键' },
                },
                {
                  path: 'wxMember-oracle-to-mongo-incremental',
                  name: 'wxMemberOracleToMongoSeqIncremental',
                  component: () => import('@/views/data/WxMemberOracleToMongoIncremental.vue'),
                  meta: { title: 'WxMember增量' },
                },
              ],
            },
          ],
        },
      ],
    },
    {
      path: '/user',
      component: UserLayout,
      children: [
        {
          path: 'login',
          name: 'login',
          component: () => import('../views/login/LoginView.vue'),
        },
      ],
    },
  ],
});

router.beforeEach((to, from) => {
  // 检查用户是否已登录
  if (!globalStore.state.loginUser.token && to.name !== 'login') {
    // 将用户重定向到登录页面
    return { name: 'login' };
  } else {
    return true;
  }
});

export default router;
